package scalab.core
import scalab.utils.Reporter
import scalab.core.interpreter.{Settings, Interpreter}

object Main{

	def main(args: Array[String]): Unit = {
		if(!(processArgs(args) && Interpreter.interpret))
			System.exit(1) //notify that build failed
			
	}

	def processArgs(args: Array[String]): Boolean = {
		var preserveCache: Boolean = false
		var bootCacheDir: String = null
		var bootThresholdSize: Long = -1L
		var bootEvictedSize: Long = -1L
		var mainBuildFile: String = null
		var targetName: String = null

		val nbArgs = args.length
		var argId = 0
		while(argId < nbArgs){
			val hasNext: Boolean = argId + 1 < nbArgs
			if(args(argId) == "--preserve-cache"){
				if(preserveCache){
					Reporter.error("Got option --preserve-cache twice!")
					return false
				}
				preserveCache = true
			}
			else if(args(argId) == "--boot-cache-dir"){
				if(bootCacheDir ne null){
					Reporter.error("Got option --boot-cache-dir twice!")
					return false
				}
				if(!hasNext){
					Reporter.error("Expected parameter value after --boot-cache-dir option!")
				}
				argId += 1
				bootCacheDir = args(argId)
			}
			else if(args(argId) == "--boot-threshold-size"){
				if(bootThresholdSize != -1L){
					Reporter.error("Got option --boot-threshold-size twice!")
					return false
				}
				if(!hasNext){
					Reporter.error("Expected parameter value after --boot-threshold-size option!")
				}
				argId += 1
				bootThresholdSize = try{
					java.lang.Long.parseLong(args(argId))
				}
				catch{
					case nfe: NumberFormatException =>
						Reporter.error(args(argId) + "is not a Long value!")
						return false
				}
			}
			else if(args(argId) == "--boot-evicted-size"){
				if(bootEvictedSize != -1L){
					Reporter.error("Got option --boot-evicted-size twice!")
					return false
				}
				if(!hasNext){
					Reporter.error("Expected parameter value after --boot-evicted-size option!")
				}
				argId += 1
				bootEvictedSize = try{
					java.lang.Long.parseLong(args(argId))
				}
				catch{
					case nfe: NumberFormatException =>
						Reporter.error(args(argId) + "is not a Long value!")
						return false
				}
			}
			else if(args(argId) == "-f"){
				if(mainBuildFile ne null){
					Reporter.error("Got option -f twice!")
					return false
				}
				if(!hasNext){
					Reporter.error("Expected parameter value after -f option!")
				}
				argId += 1
				mainBuildFile = args(argId)
			}
			else if(argId == nbArgs - 1){//target is last parameter
				targetName = args(argId)
			}
			else{
				Reporter.error("Unknown option: " + args(argId))
				return false
			}
			argId += 1
		}

		if(preserveCache) Settings.preserveCache = true
		if(bootCacheDir ne null) Settings.bootCacheDir = bootCacheDir
		if(bootThresholdSize != -1L) Settings.bootThresholdSize = bootThresholdSize
		if(bootEvictedSize != -1L) Settings.bootEvictedSize = bootEvictedSize
		if(mainBuildFile ne null) Settings.mainBuildFile = mainBuildFile
		if(targetName ne null) Settings.targetName = targetName
		return true
	}
}
